﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using WickedFlame.Core.Shapes;
using WickedFlame.Core;
using WickedFlame.Util.Data;
using System.IO;
using WickedFlame.Core.Layers;

namespace WickedFlame.Util.Actions
{
    public class DropAction
    {
        DrawingCanvas canvas;
        BitmapImage image;
        DrawingCanvas loadedCanvas;

        public DrawingCanvas Canvas
        {
            set
            {
                canvas = value;
            }
        }

        public double Width
        {
            get
            {
                if (loadedCanvas != null)
                    return loadedCanvas.Width;

                if (image != null)
                    return image.Width;

                return 0;
            }
        }

        public double Height
        {
            get
            {
                if (loadedCanvas != null)
                    return loadedCanvas.Height;

                if (image != null)
                    return image.Height;

                return 0;
            }
        }

        public void DoAction(DrawingCanvas canvas, string filename)
        {
            Debug.WriteLine("Image: " + filename + "droped and added in MainGui");

            this.canvas = canvas;
            if (canvas != null)
                canvas.SelectedLayer.UnselectAll();
            DrawingCanvas.DrawTrackers();

            if (filename.Substring(filename.LastIndexOf('.')) == ".vec")
            {
                using (Stream stream = new FileStream(
                          filename, FileMode.Open, FileAccess.Read))
                {
                    FileFormat file = new VecFileFormat();
                    if (file != null)
                        loadedCanvas = file.Import(stream);
                }
            }
            else
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                ms.SetLength(stream.Length);
                stream.Read(ms.GetBuffer(), 0, (int)stream.Length);

                ms.Flush();
                stream.Close();

                image = new BitmapImage(); ;
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = ms;
                image.EndInit();

                if (filename.Substring(filename.LastIndexOf('.')) != ".png")
                {
                    // convert to image with alphachanel
                    var action = new ConvertImageToPbgra32Action();
                    image = action.ConvertBitmapFormat(image);
                }

                // clean up the stream and delete all references 
                // to avoid file access exceptions when attempting to delete images
                image.StreamSource.Dispose();
            }
        }

        /// <summary>
        /// Finalizes the action
        /// Create the shape and adds it to the canvas
        /// </summary>
        public void FinalilzeAction()
        {
            if (loadedCanvas != null)
            {
                if (canvas == null)
                {
                    canvas = new DrawingCanvas()
                    {
                        Width = loadedCanvas.Width,
                        Height = loadedCanvas.Height
                    };
                }

                while (loadedCanvas.Layers.Count > 0)
                {
                    try
                    {
                        //CanvasLayer l = loadedCanvas.Layers[0];
                        CanvasLayer l = loadedCanvas.Layers[loadedCanvas.Layers.Count-1];
                        loadedCanvas.RemoveLayer(l);
                        canvas.AddLayer(l);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                }
                canvas.Refresh();
            }
            else
            {
                //create the shape and add to the canvas
                var shape = new ImageShape(image);
                canvas.AddShape(shape, true);
                shape.Invalidate();
            }
        }
    }
}
